using System.Reflection;
using NodeCanvas.Variables;
using UnityEngine;

namespace NodeCanvas.Actions
{
	[Category("✫ Script Control")]
	[Description("Set a variable on a script")]
	[AgentType(typeof(Transform))]
	public class SetField : ActionTask
	{
		public BBVariableSet setValue = new BBVariableSet();

		[SerializeField]
		private string fieldName;

		[SerializeField]
		private string scriptName;

		private Component script;

		private FieldInfo field;

		protected override string info
		{
			get
			{
				if (string.IsNullOrEmpty(fieldName))
				{
					return "No Field Selected";
				}
				return string.Format("{0}.{1} = {2}", base.agentInfo, fieldName, setValue.selectedBBVariable);
			}
		}

		protected override string OnInit()
		{
			script = base.agent.GetComponent(scriptName);
			if (script == null)
			{
				return "Missing Component";
			}
			field = script.GetType().NCGetField(fieldName);
			if (field == null)
			{
				return "Missing Field Info";
			}
			return null;
		}

		protected override void OnExecute()
		{
			field.SetValue(script, setValue.objectValue);
			EndAction(true);
		}
	}
}
